package handler

import (
	"cargo-admin/model"
	"github.com/gin-gonic/gin"
	"strconv"
)

func RoleAddNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	data := model.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
	})
	return
}
func RoleNodeList(c *gin.Context) {
	rid := c.Query("rid")
	var node model.Node
	/*
		SELECT
		*
		FROM
		nodes
		LEFT JOIN role_nodes ON role_nodes.nid=nodes.id
		WHERE role_nodes.nid=1
	*/
	model.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON role_nodes.nid=nodes.id").
		Where("role_nodes.nid=?", rid).
		Find(&node).Debug()
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "OK",
		"data": node,
	})
	return
}
